package com.itheima.demo04JDBC查询数据_重点;

import java.sql.*;
import java.util.ArrayList;
//万物皆对象,请注意使用对象的思维来处理问题
public class Demo04 {
	public static void main(String[] args) throws Exception {
		// 1.注册驱动(自动注册)
		// 2.获取连接
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day18", "root", "root");
		// 3.获取Statement小货车
		Statement stmt = conn.createStatement();
		// 4.执行SQL语句
		String sql = "SELECT * FROM user;";
		ResultSet rs = stmt.executeQuery(sql);
//		创建一个集合用于存储数据
		ArrayList<Student> user = new ArrayList<>();
		// 5.处理结果
		while(rs.next()){
			int id = rs.getInt("id");
			String name = rs.getString("name");
			String password = rs.getString("password");
			Student s = new Student(id, name, password);
			user.add(s);
		}
		// 6.关闭资源
		rs.close();
		stmt.close();
		conn.close();

//		循环遍历
		for (Student ss : user) {
			System.out.println(ss);
		}
	}
}
